Bugfix :: Fix F# exception serialization to preserve fields#19342
Open
Bugfix :: Fix F# exception serialization to preserve fields#19342
Conversation
Contributor
❗ Release notes required
|
c9b17c0 to
f6f7424
Compare
Member
Author
|
/azp run fsharp-ci |
|
Azure Pipelines successfully started running 1 pipeline(s). |
675d541 to
4b0a72e
Compare
Member
|
An edge case, but now this causes a runtime crash due to field name collision with open System
open System.Runtime.Serialization
exception Foo of Message:string
let e = Foo("hello")
let info = SerializationInfo(e.GetType(), FormatterConverter())
let ctx = StreamingContext(StreamingContextStates.All)
e.GetObjectData(info, ctx) |
Fixes #878 Co-authored-by: Copilot <[email protected]>
The generated GetObjectData override on F# exception types must have the [SecurityCritical] attribute to match the security accessibility of Exception.GetObjectData on .NET Framework. Without this, the CLR rejects the override with 'Inheritance security rules violated', causing FS0193 errors when any project references FSharp.Core (which contains MatchFailureException with the new GetObjectData override). Also update IL baselines and FSharp.Core surface area baseline. Co-authored-by: Copilot <[email protected]>
FSharp.Core has [assembly: SecurityTransparent] which makes all methods transparent. On .NET Framework, transparent methods cannot override SecurityCritical methods (Exception.GetObjectData) nor call SecurityCritical base constructors (Exception(SerializationInfo, StreamingContext)). Skip emitting serialization members for FSharp.Core exceptions. Co-authored-by: Copilot <[email protected]>
…, add test FSharp.Core has [assembly: SecurityTransparent]. On .NET Framework, transparent code cannot override SecurityCritical methods (GetObjectData). Without GetObjectData to write fields, the field-restoring ctor would crash. So for FSharp.Core exceptions: - Keep the base-call-only deserialization ctor (status quo, SecuritySafeCritical base) - Skip GetObjectData override (can't override SecurityCritical from transparent) For user exceptions (no SecurityTransparent): - Emit both GetObjectData and field-restoring ctor (full serialization) Add test verifying FSharp.Core MatchFailureException: - Loads without TypeLoadException - Has deserialization ctor - Does NOT have GetObjectData override Co-authored-by: Copilot <[email protected]>
The compilingFSharpCore guard skips GetObjectData, so the surface area baseline should not include it for MatchFailureException. Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
…ctors Add deserialization constructor bodies (reading fields from SerializationInfo) and GetObjectData overrides to match the netcore baselines. Co-authored-by: Copilot <[email protected]>
Changed eventsWhen condition in 'Cancel running jobs with the same key' test to wait until all 10 outdated jobs are canceled and the new job finishes, instead of stopping at the first Finished event. This prevents race conditions where assertions run before all cancellations complete. Co-authored-by: Copilot <[email protected]>
…issue #18411) Co-authored-by: Copilot <[email protected]>
Use IL backing field name (with @ suffix) as the serialization key instead of the property name. This prevents duplicate key crashes when an F# exception field name collides with a key already used by Exception.GetObjectData (e.g., 'exception Foo of Message:string'). The backing field name (e.g., 'Message@', 'Data0@') is guaranteed to not collide with base Exception serialization keys. Co-authored-by: Copilot <[email protected]>
Add Issue_878_ExceptionSerialization_MessageFieldCollision test that reproduces the exact edge case where an F# exception field named 'Message' collides with Exception.GetObjectData's 'Message' key. The test verifies: - GetObjectData does not throw with duplicate key - F# field is serialized under backing field name 'Message@' - Base Exception's 'Message' key remains accessible - Full serialization roundtrip preserves the field value Co-authored-by: Copilot <[email protected]>
- Eliminate ilBaseOnlyCtorInstrs/ilBaseOnlyCtor block that duplicated ilInstrsForSerialization/ilCtorDefForSerialization by introducing shouldRestoreFields flag - Simplify 3-branch if/elif/else to 2-branch conditional - Remove unused ilPropName parameter from emitSerializationFieldIL callback Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
1b2f39a to
2debec9
Compare
…yIL list Replace the duplicated getActualIL helper and Assert.Contains call with a proper IL fragment in the verifyIL list, verifying the AddValue callvirt instruction is emitted in the GetObjectData method. Co-authored-by: Copilot <[email protected]>
Member
Author
|
Good catch! Fixed in 234f944 — serialization keys now use the backing field name (e.g. Added a dedicated test ( |
Member
Author
|
/azp run fsharp-ci |
|
Azure Pipelines successfully started running 1 pipeline(s). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes F# exceptions losing their field values during serialization.
Exceptions with fields now emit a
GetObjectDataoverride and a deserialization constructor that save/restore the fields viaSerializationInfo, enabling correctISerializableroundtrips.Note: This fix is primarily relevant for users targeting .NET Framework 4.x or .NET Core ≤7, where
BinaryFormatter/ISerializableserialization is still available. On .NET 8+,StreamingContextis removed and the serialization members are not emitted. The entireISerializableinfrastructure is deprecated (SYSLIB0051) — no modern serializer (System.Text.Json,DataContractSerializer, etc.) usesGetObjectData.FSharp.Core exception caveat: FSharp.Core has
[assembly: SecurityTransparent], which on .NET Framework prevents overriding theSecurityCriticalmethodException.GetObjectData. FSharp.Core exceptions (e.g.MatchFailureException) therefore get only the base-call deserialization constructor (status quo) but not theGetObjectDataoverride. This is tested explicitly. User-defined exceptions are unaffected — they get full serialization support.